#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    int findPairs(vector<int>& nums, int k) {
        sort(nums.begin(),nums.end());
        int res=0;
        int left=0,right=1;
        for(;left<nums.size();left++)
        {
            if(!left||nums[left]!=nums[left-1])
            {
                while(right<nums.size()&&((nums[right]<nums[left]+k)||right<=left))
                    right++;
                if(right<nums.size()&&nums[right]==nums[left]+k)res++;
            }
        }
        return res;
    }
};